extends Node2D

@onready var animated_sprite = $AnimatedSprite2D
@onready var timer = $Timer

var weapon_name: String = ""
var float_tween: Tween

# 武器动画帧资源字典
var weapon_animations = {
	"裁星者": "caixingzhe_sprite_frames"
}

# 默认纹理路径
var default_textures = {
	"裁星者": "res://assets/ColorfulSword_idle.png"
}

func _ready():
	print("武器特效节点初始化")
	print("AnimatedSprite2D节点: ", animated_sprite != null)
	print("Timer节点: ", timer != null)
	
	if animated_sprite:
		print("动画帧: ", animated_sprite.sprite_frames != null)
		if animated_sprite.sprite_frames:
			print("可用动画: ", animated_sprite.sprite_frames.get_animation_names())
	
	# 连接计时器信号
	if timer:
		timer.timeout.connect(_on_timer_timeout)
	else:
		print("错误: Timer节点不存在")
	
	# 初始化悬浮动画
	_start_float_animation()

# 设置武器名称并加载对应的动画
func set_weapon(name: String):
	weapon_name = name
	print("设置武器特效: ", name)
	
	# 检查AnimatedSprite2D节点
	if not animated_sprite:
		print("错误: AnimatedSprite2D节点不存在")
		return
	
	# 检查sprite_frames
	if not animated_sprite.sprite_frames:
		print("错误: sprite_frames不存在")
		_use_default_texture()
		return
	
	# 查找对应的动画资源
	if weapon_name in weapon_animations:
		var animation_name = weapon_animations[weapon_name]
		print("尝试播放动画: ", animation_name)
		print("可用动画: ", animated_sprite.sprite_frames.get_animation_names())
		
		if animated_sprite.sprite_frames.has_animation(animation_name):
			animated_sprite.play(animation_name)
			print("成功播放武器动画: ", animation_name)
		else:
			print("警告: 找不到武器动画: ", animation_name)
			_use_default_texture()
	else:
		print("警告: 未定义武器动画: ", weapon_name)
		_use_default_texture()

# 使用默认纹理
func _use_default_texture():
	print("尝试使用默认纹理")
	
	# 检查是否有默认纹理
	if weapon_name in default_textures:
		var texture_path = default_textures[weapon_name]
		print("默认纹理路径: ", texture_path)
		
		if ResourceLoader.exists(texture_path):
			print("默认纹理存在，正在加载")
			# 创建精灵节点替换动画精灵
			var sprite = Sprite2D.new()
			sprite.texture = load(texture_path)
			sprite.scale = Vector2(1.2, 1.2)
			
			# 替换节点 - 添加null检查
			if animated_sprite and is_instance_valid(animated_sprite):
				print("替换现有的AnimatedSprite2D节点")
				var parent = animated_sprite.get_parent()
				var pos = animated_sprite.position
				
				if parent and is_instance_valid(parent):
					print("父节点存在，正在替换")
					parent.remove_child(animated_sprite)
					parent.add_child(sprite)
					sprite.position = pos
				else:
					# 如果没有父节点，直接添加到自身
					print("警告: 找不到父节点，直接添加到自身")
					add_child(sprite)
				
				# 更新引用
				animated_sprite = sprite
			else:
				# 如果animated_sprite无效，直接添加到自身
				print("警告: AnimatedSprite2D无效，直接添加到自身")
				add_child(sprite)
				animated_sprite = sprite
			
			print("默认纹理设置完成")
		else:
			print("错误: 默认纹理不存在: ", texture_path)
	else:
		print("错误: 未定义默认纹理: ", weapon_name)

# 启动上下悬浮动画
func _start_float_animation():
	
	print("启动悬浮动画")
	
	# 取消之前的动画
	if float_tween and float_tween.is_valid():
		float_tween.kill()
	
	# 创建新的悬浮动画
	float_tween = create_tween()
	float_tween.set_loops()  # 无限循环
	
	# 上下浮动
	var start_y = position.y
	float_tween.tween_property(self, "position:y", start_y - 10, 1.0).set_ease(Tween.EASE_IN_OUT)
	float_tween.tween_property(self, "position:y", start_y + 10, 1.0).set_ease(Tween.EASE_IN_OUT)
	
	print("悬浮动画已启动")

# 计时器超时处理
func _on_timer_timeout():
	print("计时器超时")
	
	# 检查是否已设置动画或纹理
	if animated_sprite and animated_sprite is AnimatedSprite2D and not animated_sprite.sprite_frames:
		print("AnimatedSprite2D没有sprite_frames，使用默认纹理")
		_use_default_texture()
	else:
		print("AnimatedSprite2D状态正常") 
